<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8" />
    
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    Advanced_JS_two.md |  Small Bridge
  </title>
  <meta name="generator" content="hexo-theme-yilia-plus">
  
  <link rel="shortcut icon" href="/favicon.ico" />
  
  
<link rel="stylesheet" href="/css/main.css">

  
  <script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>
  
  

  

<link rel="alternate" href="/atom.xml" title="Small Bridge" type="application/atom+xml">
</head>

</html>

<body>
  <div id="app">
    <main class="content">
      <section class="outer">
  <article id="post-Advanced-JS-two-md" class="article article-type-post" itemscope
  itemprop="blogPost" data-scroll-reveal>

  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  Advanced_JS_two.md
</h1>
  

    </header>
    

    
    <div class="article-meta">
      <a href="/2020/06/13/Advanced-JS-two-md/" class="article-date">
  <time datetime="2020-06-13T13:38:54.000Z" itemprop="datePublished">2020-06-13</time>
</a>
      
  <div class="article-category">
    <a class="article-category-link" href="/categories/JavaScript/">JavaScript</a>
  </div>

      
      
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">4k字</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">16分钟</span>
        </span>
    </span>
</div>

      
    </div>
    

    
    
    <div class="tocbot"></div>





    

    
    <div class="article-entry" itemprop="articleBody">
      


      

      
      <p>@<a href="函数高级">TOC</a></p>
<h1 id="原型和原型链"><a href="#原型和原型链" class="headerlink" title="原型和原型链"></a>原型和原型链</h1><a id="more"></a>
<p>当我们在JS中使用构造函数new出一个实例对象时，这个对象上就会有一些方法，比如toString()等，你没有没想过这些方法是哪儿来的？原构造函数上面的？不是，其实它是来自构造函数的原型对象上的，下面我们来看看关于JS原型</p>
<ol>
<li><p>函数的prototype属性<br> 每个函数都存在一个prototype属性，它默认指向一个空的Object实例对象(即称为原型对象)<br> 原型对象中有一个属性constructor，它执行函数对象<br> 给原型对象添加属性，函数的所有实例对象自动拥有原型中的属性</p>
</li>
<li><p>显式原型和隐式原型<br> 每个函数function都有一个prototype，即显式原型属性<br> 每个实例对象都有一个<strong>proto</strong>,即隐式原型属性<br> <code>对象的隐式原型属性的值等于其构造函数的显式原型属性的值</code>这句话非常重要，务必牢记</p>
</li>
<li><p>原型链<br>访问一个对象的属性的时候，</p>
<pre><code>先在自身属性中查找，找到返回
如果没有，再沿着__proto__这条链向上寻找，找到返回
如果没有找到，返回undefined
别名：隐式原型链
作用：查找对象的属性(方法)</code></pre><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">&lt;script type=<span class="string">"text/javascript"</span>&gt;</span><br><span class="line">		<span class="function"><span class="keyword">function</span> <span class="title">Fn</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">			<span class="keyword">this</span>.test1 = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">				<span class="built_in">console</span>.log(<span class="string">'test1()'</span>)</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		Fn.prototype.test2 = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">			<span class="built_in">console</span>.log(<span class="string">'test2()'</span>)</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">var</span> fn = <span class="keyword">new</span> Fn()</span><br><span class="line">		</span><br><span class="line">		fn.test1()</span><br><span class="line">		fn.test2()</span><br><span class="line">		<span class="built_in">console</span>.log(fn.toString())</span><br><span class="line">		fn.test3()</span><br><span class="line">		</span><br><span class="line">		<span class="comment">// 1. 函数的显式原型指向的对象默认是空Object实例对象(但Object不满足)</span></span><br><span class="line">		<span class="built_in">console</span>.log(Fn.prototype <span class="keyword">instanceof</span> <span class="built_in">Object</span>)   <span class="comment">//true</span></span><br><span class="line">		<span class="built_in">console</span>.log(<span class="built_in">Object</span>.prototype <span class="keyword">instanceof</span> <span class="built_in">Object</span>) <span class="comment">//false</span></span><br><span class="line">		<span class="built_in">console</span>.log(<span class="built_in">Function</span>.prototype <span class="keyword">instanceof</span> <span class="built_in">Object</span>) <span class="comment">//true</span></span><br><span class="line">		</span><br><span class="line">		<span class="comment">//2.所有函数都是Function的实例(包括Function自己)</span></span><br><span class="line">		<span class="built_in">console</span>.log(<span class="built_in">Function</span>.__proto__ === <span class="built_in">Function</span>.prototype)</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//3.Object的原型对象是原型链的尽头</span></span><br><span class="line">		<span class="built_in">console</span>.log(<span class="built_in">Object</span>.prototype.__proto__) <span class="comment">//null</span></span><br><span class="line">	&lt;<span class="regexp">/script&gt;</span></span><br></pre></td></tr></table></figure>
<p>就上面的代码，我们画一下内存结构图(图是我手画的，画得不好)<br><img src="https://img-blog.csdnimg.cn/20200530212058624.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0NjI1MTQy,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"><br>我们可以看到当调用对象的属性的时候，会沿着隐式原型链进行查找，test1在原本对象上，可以调用，test2在原型对象上，可以看到，同理toString方法也可以看到<br>我们需要理解三句话<br>a. 函数的显式原型指向的对象默认是空Object实例对象(但Object不满足)<br>所有函数都满足，唯独Object，其函数对象指向的是Object原型对象</p>
</li>
</ol>
<p>b.所有函数都是Function的实例(包括Function自己)<br>在JS中，函数也被视为对象，那么所有函数对象的构造函数都是Function,包括它自己</p>
<p>c.Object的原型对象是原型链的尽头<br>在图中，我们可以清晰看到它的隐式原型属性为null</p>
<p>为此我们再看一张图(我把里面的constructor属性省略了，因为加上太乱了)<br><img src="https://img-blog.csdnimg.cn/20200530223854563.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0NjI1MTQy,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<ol start="4">
<li><p>原型链属性问题<br>a.读取对象的属性值时，会自动到原型链中查找<br>b.设置对象的属性值时，不会查找原型链，如果当前对象没有此属性，直接添加此属性并设置值<br>c.方法一般定义在原型中，属性一般通过构造函数定义在对象本身上</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">&lt;script type=<span class="string">"text/javascript"</span>&gt;</span><br><span class="line">		<span class="function"><span class="keyword">function</span>  <span class="title">Fn</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">			</span><br><span class="line">		&#125;</span><br><span class="line">		Fn.prototype.a = <span class="string">'xxx'</span></span><br><span class="line">		<span class="keyword">var</span> fn1 = <span class="keyword">new</span> Fn()</span><br><span class="line">		<span class="built_in">console</span>.log(fn1.a,fn1)</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">var</span> fn2 = <span class="keyword">new</span> Fn()</span><br><span class="line">		fn2.a = <span class="string">'yyy'</span></span><br><span class="line">		<span class="built_in">console</span>.log(fn1.a,fn2.a,fn2)</span><br><span class="line">	&lt;<span class="regexp">/script&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>探索instanceof<br>instanceof是如何判断的？<br>表达式：A instanceof B<br>如果B函数的显式原型对象在A对象的原型链上，返回true，否则返回false</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">&lt;script type=<span class="string">"text/javascript"</span>&gt;</span><br><span class="line">		<span class="function"><span class="keyword">function</span> <span class="title">Foo</span>(<span class="params"></span>)</span>&#123;&#125;</span><br><span class="line">		<span class="keyword">var</span> f1 = <span class="keyword">new</span> Foo()</span><br><span class="line">		<span class="built_in">console</span>.log(f1 <span class="keyword">instanceof</span> Foo) <span class="comment">//true</span></span><br><span class="line">		<span class="built_in">console</span>.log(f1 <span class="keyword">instanceof</span> <span class="built_in">Object</span>) <span class="comment">//true</span></span><br><span class="line">		</span><br><span class="line">		</span><br><span class="line">		</span><br><span class="line">		<span class="built_in">console</span>.log(<span class="built_in">Object</span> <span class="keyword">instanceof</span> <span class="built_in">Function</span>) <span class="comment">//true</span></span><br><span class="line">		<span class="built_in">console</span>.log(<span class="built_in">Object</span> <span class="keyword">instanceof</span> <span class="built_in">Object</span>) <span class="comment">//true</span></span><br><span class="line">		<span class="built_in">console</span>.log(<span class="built_in">Function</span> <span class="keyword">instanceof</span> <span class="built_in">Function</span>) <span class="comment">//true</span></span><br><span class="line">		<span class="built_in">console</span>.log(<span class="built_in">Function</span> <span class="keyword">instanceof</span> <span class="built_in">Object</span>) <span class="comment">//true</span></span><br><span class="line">		</span><br><span class="line">		<span class="function"><span class="keyword">function</span> <span class="title">Fo</span>(<span class="params"></span>) </span>&#123;&#125;</span><br><span class="line">		<span class="built_in">console</span>.log(<span class="built_in">Object</span> <span class="keyword">instanceof</span> Fo) <span class="comment">//false</span></span><br><span class="line">	&lt;<span class="regexp">/script&gt;</span></span><br></pre></td></tr></table></figure>
<p>画图就交给你自己画了，动手画一下吧</p>
</li>
<li><p>分享2个面试题</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">&lt;script type=<span class="string">"text/javascript"</span>&gt;</span><br><span class="line">	<span class="comment">// 测试题1</span></span><br><span class="line">	<span class="keyword">var</span> A = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line">	A.prototype.n = <span class="number">1</span></span><br><span class="line">	</span><br><span class="line">	<span class="keyword">var</span> b = <span class="keyword">new</span> A()</span><br><span class="line">	</span><br><span class="line">	A.prototype = &#123;</span><br><span class="line">		n: <span class="number">2</span>,</span><br><span class="line">		m: <span class="number">3</span></span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">var</span> c = <span class="keyword">new</span> A()</span><br><span class="line">	<span class="built_in">console</span>.log(b.n,b.m,c.n,c.m) <span class="comment">// 1 undefined 2 3</span></span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//测试题2</span></span><br><span class="line">	<span class="keyword">var</span> F = <span class="function"><span class="keyword">function</span> (<span class="params"></span>)</span>&#123;</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">Object</span>.prototype.a = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">		<span class="built_in">console</span>.log(<span class="string">'a()'</span>)</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">Function</span>.prototype.b = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">		<span class="built_in">console</span>.log(<span class="string">'b()'</span>)</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">var</span> f = <span class="keyword">new</span> F()</span><br><span class="line">	f.a()</span><br><span class="line">	<span class="comment">//f.b()</span></span><br><span class="line">	F.a()</span><br><span class="line">	F.b()</span><br><span class="line">   &lt;<span class="regexp">/script&gt;</span></span><br></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/2020053022211635.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0NjI1MTQy,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"><br>第二个自己动手画画吧！自己动手画一次，知识才是你的</p>
<h1 id="执行上下文与执行上下文栈"><a href="#执行上下文与执行上下文栈" class="headerlink" title="执行上下文与执行上下文栈"></a>执行上下文与执行上下文栈</h1></li>
<li><p>变量提升与函数提升<br>变量声明提升<br>通过var定义的变量，在定义语句之前就可以访问，不过值是undefined<br>函数声明提升<br>通过function声明的函数，在之前就可以直接调用</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">&lt;script type=<span class="string">"text/javascript"</span>&gt;</span><br><span class="line">		<span class="keyword">var</span> a = <span class="number">3</span></span><br><span class="line">		<span class="function"><span class="keyword">function</span> <span class="title">fn</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">			<span class="built_in">console</span>.log(a)  <span class="comment">//undefined</span></span><br><span class="line">			<span class="keyword">var</span> a = <span class="number">4</span></span><br><span class="line">			<span class="comment">//相当于写</span></span><br><span class="line">			<span class="comment">//var a console.log(a) a = 4</span></span><br><span class="line">		&#125;</span><br><span class="line">		fn()</span><br><span class="line">		</span><br><span class="line">		<span class="built_in">console</span>.log(b)  <span class="comment">//变量提升 undefined</span></span><br><span class="line">		fn2()  <span class="comment">//可调用 函数提升</span></span><br><span class="line">		<span class="comment">//fn3()  不能 变量提升(函数表达式声明不会函数提升)</span></span><br><span class="line">		</span><br><span class="line">		<span class="keyword">var</span> b = <span class="number">3</span></span><br><span class="line">		<span class="function"><span class="keyword">function</span> <span class="title">fn2</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">			<span class="built_in">console</span>.log(<span class="string">'fn2()'</span>)</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">var</span> fn3 = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">			<span class="built_in">console</span>.log(<span class="string">'fn3()'</span>)</span><br><span class="line">		&#125;</span><br><span class="line">	&lt;<span class="regexp">/script&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>执行上下文<br>在看这个概念之前，我们先来看看代码分类<br>代码依据位置进行分类:全局代码   局部代码,以此衍生了两个概念，全局执行上下文，函数执行上下文,他们是对象</p>
</li>
</ol>
<p>a.全局执行上下文<br>在执行全局代码前将window确定为全局执行上下文<br>对全局数据进行预处理:<br>    var定义的全局变量===&gt;undefined,添加为window的属性<br>    function声明的全局函数===&gt;赋值，添加为window的方法<br>    this===&gt;赋值给window<br>然后开始执行全局代码</p>
<p>b.函数执行上下文<br>在调用函数，准备执行函数体之前，创建对应的函数执行上下文对象(虚拟的，存在于栈中)<br>对局部数据进行预处理:<br>形参变量===&gt;赋值(实参)===&gt;添加为执行上下文的属性<br>arguments===&gt;赋值(实参列表),添加为执行上下文的属性<br>var定义的局部变量===&gt;undefined，添加为执行上下文的属性<br>function声明的函数===&gt;赋值，添加为执行上下文的方法<br>this===&gt;赋值(调用函数的对象)<br>然后开始执行函数体代码</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">&lt;script type=<span class="string">"text/javascript"</span>&gt;</span><br><span class="line">		<span class="comment">//全局执行上下文</span></span><br><span class="line">		<span class="built_in">console</span>.log(a1,<span class="built_in">window</span>.a1)</span><br><span class="line">		a2()</span><br><span class="line">		<span class="built_in">console</span>.log(<span class="keyword">this</span>)</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">var</span> a1 = <span class="number">3</span></span><br><span class="line">		<span class="function"><span class="keyword">function</span> <span class="title">a2</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">			<span class="built_in">console</span>.log(<span class="string">'a2()'</span>)</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		</span><br><span class="line">		<span class="built_in">console</span>.log(<span class="string">"--------------"</span>)</span><br><span class="line">		<span class="comment">//函数执行上下文</span></span><br><span class="line">		<span class="function"><span class="keyword">function</span> <span class="title">fn</span>(<span class="params">a1</span>)</span>&#123;</span><br><span class="line">			<span class="built_in">console</span>.log(a1) <span class="comment">//2</span></span><br><span class="line">			<span class="built_in">console</span>.log(a2)  <span class="comment">//undefined</span></span><br><span class="line">			a3()             <span class="comment">//a3()</span></span><br><span class="line">		    <span class="built_in">console</span>.log(<span class="keyword">this</span>) <span class="comment">//window </span></span><br><span class="line">			<span class="built_in">console</span>.log(<span class="built_in">arguments</span>) <span class="comment">//伪数组 (2,3)</span></span><br><span class="line">			</span><br><span class="line">			<span class="keyword">var</span> a2 = <span class="number">3</span></span><br><span class="line">			<span class="function"><span class="keyword">function</span> <span class="title">a3</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">				<span class="built_in">console</span>.log(<span class="string">'a3()'</span>)</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		fn(<span class="number">2</span>,<span class="number">3</span>)</span><br><span class="line">	&lt;<span class="regexp">/script&gt;</span></span><br></pre></td></tr></table></figure>

<ol start="3">
<li>执行上下文栈<br>1.在全局代码执行前，JS引擎就会创建一个栈来存储管理所有的执行上下文对象<br>2.在全局执行上下文(Window)确定后，将其添加到栈中(压栈)</li>
<li>在函数执行上下文创建后，将其添加到栈中(压栈)</li>
<li>在当前函数执行完后，将栈顶的对象移除(出栈)</li>
<li>当所有的代码执行完后，栈中只剩下Window<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&lt;script type=<span class="string">"text/javascript"</span>&gt;</span><br><span class="line">		                          <span class="comment">//1.进入全局执行上下文</span></span><br><span class="line">		<span class="keyword">var</span> a = <span class="number">10</span></span><br><span class="line">		<span class="keyword">var</span> bar = <span class="function"><span class="keyword">function</span>(<span class="params">x</span>)</span>&#123;</span><br><span class="line">			<span class="keyword">var</span> b = <span class="number">5</span></span><br><span class="line">			foo(x + b)            <span class="comment">//3.进入foo执行上下文</span></span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">var</span> foo = <span class="function"><span class="keyword">function</span>(<span class="params">y</span>)</span>&#123;</span><br><span class="line">			<span class="keyword">var</span> c = <span class="number">5</span></span><br><span class="line">			<span class="built_in">console</span>.log(a + c + y)</span><br><span class="line">		&#125;</span><br><span class="line">		bar(<span class="number">10</span>)                   <span class="comment">//2.进入bar函数执行上下文</span></span><br><span class="line">	&lt;<span class="regexp">/script&gt;</span></span><br></pre></td></tr></table></figure>
<img src="https://img-blog.csdnimg.cn/20200530230755231.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0NjI1MTQy,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></li>
</ol>
<p>我们看3道面试题</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line">&lt;script type=<span class="string">"text/javascript"</span>&gt;</span><br><span class="line">		<span class="built_in">console</span>.log(<span class="string">'global begin:'</span> + i)</span><br><span class="line">		<span class="keyword">var</span> i = <span class="number">1</span></span><br><span class="line">		foo(<span class="number">1</span>)</span><br><span class="line">		<span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params">i</span>)</span>&#123;</span><br><span class="line">			<span class="keyword">if</span>(i == <span class="number">4</span>)&#123;</span><br><span class="line">				<span class="keyword">return</span></span><br><span class="line">			&#125;</span><br><span class="line">		<span class="built_in">console</span>.log(<span class="string">'foo() begin'</span> + i)</span><br><span class="line">		foo(i + <span class="number">1</span>)</span><br><span class="line">		<span class="built_in">console</span>.log(<span class="string">'foo() end'</span> + i)</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="built_in">console</span>.log(<span class="string">'global end:'</span> + i)</span><br><span class="line">		<span class="comment">/*</span></span><br><span class="line"><span class="comment">		global begin:undefined</span></span><br><span class="line"><span class="comment">		foo() begin1</span></span><br><span class="line"><span class="comment">		foo() begin2</span></span><br><span class="line"><span class="comment">		foo() begin3</span></span><br><span class="line"><span class="comment">		foo() end3</span></span><br><span class="line"><span class="comment">		foo() end2</span></span><br><span class="line"><span class="comment">		foo() end1</span></span><br><span class="line"><span class="comment">		global end:1</span></span><br><span class="line"><span class="comment">		*/</span></span><br><span class="line">		</span><br><span class="line">		<span class="comment">//测试题1  先执行变量提升，再执行函数提升</span></span><br><span class="line">		<span class="function"><span class="keyword">function</span> <span class="title">a</span>(<span class="params"></span>)</span>&#123;&#125;</span><br><span class="line">		<span class="keyword">var</span> a</span><br><span class="line">		<span class="built_in">console</span>.log(<span class="keyword">typeof</span> a)  <span class="comment">// 'function'</span></span><br><span class="line">		</span><br><span class="line">		<span class="comment">//测试题2</span></span><br><span class="line">		<span class="keyword">if</span> (!(b <span class="keyword">in</span> <span class="built_in">window</span>))&#123; <span class="comment">//true</span></span><br><span class="line">			<span class="keyword">var</span> b = <span class="number">1</span></span><br><span class="line">		&#125;</span><br><span class="line">		<span class="built_in">console</span>.log(b) <span class="comment">//undefined</span></span><br><span class="line">		</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//测试题3 出得他娘的是真的好啊！！！</span></span><br><span class="line">		<span class="keyword">var</span> c = <span class="number">1</span>   <span class="comment">//相当于 var c</span></span><br><span class="line">		<span class="function"><span class="keyword">function</span> <span class="title">c</span>(<span class="params">c</span>)</span>&#123;</span><br><span class="line">			<span class="built_in">console</span>.log(c)</span><br><span class="line">			<span class="keyword">var</span> c = <span class="number">3</span></span><br><span class="line">		&#125;</span><br><span class="line">		<span class="comment">// c = 1  那c肯定不是函数了</span></span><br><span class="line">		c(<span class="number">2</span>) <span class="comment">// 报错 c不是一个函数</span></span><br><span class="line">	&lt;<span class="regexp">/script&gt;</span></span><br></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/20200530231901940.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0NjI1MTQy,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"><br>第一个的栈图，希望对你理解有帮助</p>
<h1 id="作用域与作用域链"><a href="#作用域与作用域链" class="headerlink" title="作用域与作用域链"></a>作用域与作用域链</h1><ol>
<li><p>理解<br>它就是一块”地盘”，一个代码所在的区域,它是静态的(相对于上下文对象)，在编写代码时就确定了</p>
</li>
<li><p>分类<br>全局作用域  局部作用域  块级作用域(ES6才有的)</p>
</li>
<li><p>作用<br>隔离变量，不同作用域下同名变量不会有冲突</p>
</li>
<li><p>作用域与执行上下文</p>
</li>
<li><p>区别1</p>
<pre><code>   *全局作用域之内，每个函数都会创建自己的作用域，作用域在函数定义时就已经确定了，而不是在函数调用时
   *全局执行上下文环境是在全局作用域确定之后，js代码马上执行之前创建
   *函数执行上下文是在调用函数时，函数体代码执行之前创建
2.区别2
   *作用域是静态的，只要函数定义好了就一直存在，且不再变化
   *执行上下文是动态的，调用函数时创建，函数调用结束就会自动释放
3.联系
   *上下文环境(对象)是从属于所在的作用域
   *全局上下文环境 ===&gt; 全局作用域
   *函数上下文环境 ===&gt; 对应的函数作用域</code></pre></li>
<li><p>作用域链</p>
</li>
<li><p>理解</p>
<pre><code>    *多个上下级关系的作用域形成的链，它的方向是从上向下的(从内到外)
    *查找变量时就是沿着作用域链来查找的
2.查找一个变量的查找规则
    *在当前作用域下的执行上下文中查找对应的属性，如果有直接返回，否则进入2
    *在上一级作用域的执行上下文中查找对应的属性，如果有直接返回，否则进入3
    *再次执行2的相同操作，直到全局作用域，如果还找不到就抛出找不到的异常</code></pre><h1 id="闭包"><a href="#闭包" class="headerlink" title="闭包"></a>闭包</h1></li>
<li><p>引入<br>我们通过一个循环遍历加监听的例子来看看闭包吧</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">&lt;script type=<span class="string">"text/javascript"</span>&gt;</span><br><span class="line">		<span class="keyword">var</span> btns = <span class="built_in">document</span>.querySelectorAll(<span class="string">"button"</span>)</span><br><span class="line">		<span class="comment">//比for(var i = 0; i &lt; btns.length; i++)这样写效率更高，因为btns是一个伪数组，每次循环它的length属性都要重新计算</span></span><br><span class="line">		<span class="comment">// for(var i = 0,length = btns.length; i &lt; length; i++)&#123;</span></span><br><span class="line">		<span class="comment">// 	btns[i].index = i</span></span><br><span class="line">		<span class="comment">// 	btns[i].onclick = function()&#123;</span></span><br><span class="line">		<span class="comment">// 		alert(this.index)</span></span><br><span class="line">		<span class="comment">// 	&#125;</span></span><br><span class="line">		<span class="comment">// &#125;</span></span><br><span class="line">		</span><br><span class="line">		<span class="comment">//利用闭包</span></span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">var</span> i = <span class="number">0</span>,length = btns.length; i &lt; length; i++)&#123;</span><br><span class="line">			(<span class="function"><span class="keyword">function</span>(<span class="params">i</span>)</span>&#123;</span><br><span class="line">				btns[i].onclick = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">					alert(<span class="string">`这是第<span class="subst">$&#123;i+<span class="number">1</span>&#125;</span>个`</span>);</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;)(i)</span><br><span class="line">		&#125;</span><br><span class="line">	&lt;<span class="regexp">/script&gt;</span></span><br></pre></td></tr></table></figure></li>
<li><p>理解闭包</p>
<p>如何产生闭包？<br>嵌套的内部(子)函数引用了嵌套的外部(父)函数的变量时，就产生闭包</p>
<p>闭包到底是什么？<br>闭包就是嵌套的内部函数</p>
<p>禅城闭包的条件？<br>函数嵌套，内部函数引用了外部函数的数据，内部执行函数定义就会产生闭包，不用调用内部函数，但要调用外部函数</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&lt;script type=<span class="string">"text/javascript"</span>&gt;</span><br><span class="line">	<span class="function"><span class="keyword">function</span> <span class="title">f1</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">		<span class="keyword">var</span> a = <span class="number">2</span></span><br><span class="line">		<span class="keyword">var</span> b = <span class="string">'adc'</span></span><br><span class="line">		<span class="function"><span class="keyword">function</span> <span class="title">f2</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">			<span class="built_in">console</span>.log(a)</span><br><span class="line">		&#125;</span><br><span class="line">	   <span class="comment">// f2()</span></span><br><span class="line">	&#125;</span><br><span class="line">	f1()</span><br><span class="line">&lt;<span class="regexp">/script&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>常见的闭包<br>a.将函数作为另一个函数的返回值<br>b.将函数作为实参传递给另一个函数调用</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">&lt;script type=<span class="string">"text/javascript"</span>&gt;</span><br><span class="line">	<span class="comment">//1.将函数作为另一个函数的返回值</span></span><br><span class="line">	<span class="function"><span class="keyword">function</span> <span class="title">fn1</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">		<span class="keyword">var</span> a = <span class="number">2</span></span><br><span class="line">		<span class="function"><span class="keyword">function</span> <span class="title">fn2</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">			a++</span><br><span class="line">			<span class="built_in">console</span>.log(a)</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> fn2</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">var</span> f = fn1()</span><br><span class="line">	f() <span class="comment">//3</span></span><br><span class="line">	f() <span class="comment">//4</span></span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//2.将函数作为实参传递给另一个函数调用</span></span><br><span class="line">	<span class="function"><span class="keyword">function</span> <span class="title">showDelay</span>(<span class="params">msg,time</span>)</span>&#123;</span><br><span class="line">		setTimeout(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">			alert(msg)</span><br><span class="line">		&#125;,time)</span><br><span class="line">	&#125;</span><br><span class="line">	showDelay(<span class="string">'cxq'</span>,<span class="number">1000</span>)</span><br><span class="line">&lt;<span class="regexp">/script&gt;</span></span><br></pre></td></tr></table></figure></li>
<li><p>闭包的作用</p>
</li>
</ol>
<p>问题：<br>1.函数执行完后，函数内部声明的局部变量是否还存在？<br>一般是不会存在的，存在于闭包中的变量才可能存在</p>
<p>2.在函数外部能直接访问函数内部的局部变量吗？<br>不能，但我们可以通过闭包让外部操作它</p>
<p>a.使用函数内部的变量在函数执行完后，仍然存活在内存中<br>b.让函数外部可以操作(读/写)到函数内的数据(变量/函数)</p>
<ol start="5">
<li><p>闭包的生命周期</p>
<p>1.产生：在嵌套内部函数定义执行完时就产生了(不是在调用)</p>
</li>
<li><p>死亡：在嵌套的内部函数成为垃圾对象时</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">&lt;script type=<span class="string">"text/javascript"</span>&gt;</span><br><span class="line">		<span class="function"><span class="keyword">function</span> <span class="title">fn1</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">			<span class="comment">//此时闭包就已经产生了(函数提升，内部函数对象已经创建了)</span></span><br><span class="line">			<span class="keyword">var</span> a = <span class="number">2</span></span><br><span class="line">			<span class="function"><span class="keyword">function</span> <span class="title">fn2</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">				a++</span><br><span class="line">				<span class="built_in">console</span>.log(a)</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">return</span> fn2</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">var</span> f = fn1()</span><br><span class="line">		f() <span class="comment">//3</span></span><br><span class="line">		f() <span class="comment">//4</span></span><br><span class="line">		f = <span class="literal">null</span> <span class="comment">//闭包死亡(包含闭包的函数对象成为垃圾对象)</span></span><br><span class="line">	&lt;<span class="regexp">/script&gt;</span></span><br></pre></td></tr></table></figure></li>
<li><p>闭包的应用</p>
</li>
</ol>
<p>自定义JS模块<br>a.模块是什么?<br>具有特定功能的JS文件<br>将所有的数据和功能都封装在一个函数内部<br>只向外暴露一个包含n个方法的对象或者函数<br>模块的使用者，只需要通过模块暴露的对象调用方法来实现对应的功能<br>两种方式定义模块(普通和IIFE)</p>
<p>普通</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">myMoudle</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	<span class="comment">//私有数据</span></span><br><span class="line">	<span class="keyword">var</span> msg = <span class="string">'My name'</span></span><br><span class="line">	<span class="comment">//操作数据的函数</span></span><br><span class="line">	<span class="function"><span class="keyword">function</span> <span class="title">doSomething</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">		<span class="built_in">console</span>.log(<span class="string">'doSomething()'</span> + msg.toUpperCase())</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">function</span> <span class="title">doOtherthing</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">		<span class="built_in">console</span>.log(<span class="string">'doOtherthing'</span> + msg.toLowerCase())</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//向外暴露对象(给外部函数使用的方法)</span></span><br><span class="line">	<span class="keyword">return</span>&#123;</span><br><span class="line">		doSomething: doSomething,</span><br><span class="line">		doOtherthing: doOtherthing</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;script src=<span class="string">"myMoudle.js"</span> type=<span class="string">"text/javascript"</span> charset=<span class="string">"utf-8"</span>&gt;&lt;<span class="regexp">/script&gt;</span></span><br><span class="line"><span class="regexp">&lt;script type="text/</span>javascript<span class="string">"&gt;</span></span><br><span class="line"><span class="string">		var moudle = myMoudle()</span></span><br><span class="line"><span class="string">		moudle.doSomething()</span></span><br><span class="line"><span class="string">&lt;/script&gt;</span></span><br></pre></td></tr></table></figure>

<p>IIFE</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">(<span class="function"><span class="keyword">function</span>(<span class="params">window</span>)</span>&#123;</span><br><span class="line">	<span class="comment">//私有数据</span></span><br><span class="line">	<span class="keyword">var</span> msg = <span class="string">'My name'</span></span><br><span class="line">	<span class="comment">//操作数据的函数</span></span><br><span class="line">	<span class="function"><span class="keyword">function</span> <span class="title">doSomething</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">		<span class="built_in">console</span>.log(<span class="string">'doSomething()'</span> + msg.toUpperCase())</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">function</span> <span class="title">doOtherthing</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">		<span class="built_in">console</span>.log(<span class="string">'doOtherthing'</span> + msg.toLowerCase())</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//向外暴露对象(给外部函数使用的方法)</span></span><br><span class="line">	<span class="built_in">window</span>.myMoudle2 = &#123;</span><br><span class="line">		doSomething: doSomething,</span><br><span class="line">		doOtherthing: doOtherthing</span><br><span class="line">	&#125;</span><br><span class="line">&#125;)(<span class="built_in">window</span>)</span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;script src=<span class="string">"myMoudle2.js"</span> type=<span class="string">"text/javascript"</span> charset=<span class="string">"utf-8"</span>&gt;&lt;<span class="regexp">/script&gt;</span></span><br><span class="line"><span class="regexp">&lt;script type="text/</span>javascript<span class="string">"&gt;</span></span><br><span class="line"><span class="string">		myMoudle2.doOtherthing()</span></span><br><span class="line"><span class="string">		myMoudle2.doSomething()</span></span><br><span class="line"><span class="string">&lt;/script&gt;</span></span><br></pre></td></tr></table></figure>
<ol start="7">
<li>闭包的缺点和解决</li>
</ol>
<p>我们先了解两个概念：内存溢出和内存泄露</p>
<p>内存溢出<br>一种程序运行出现的错误<br>当程序运行需要的内存超过了剩余的内存时，就抛出内存溢出的错误</p>
<p>内存泄露<br>占用内存，没有及时释放<br>内存泄露积累多了就容易导致内存溢出<br>常见的内存泄露：<br>意外的全局变量  没有及时清理的计时器 闭包未释放</p>
<p>缺点:<br>函数执行完后，函数内的局部变量没有释放，占用内存时间会变长<br>容易造成内存泄露<br>解决:<br>能不用闭包就不用<br>即时释放</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&lt;script type=<span class="string">"text/javascript"</span>&gt;</span><br><span class="line">		<span class="function"><span class="keyword">function</span> <span class="title">fn1</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">			<span class="keyword">var</span> arr = <span class="keyword">new</span> <span class="built_in">Array</span>[<span class="number">100000</span>]</span><br><span class="line">			<span class="function"><span class="keyword">function</span> <span class="title">fn2</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">				<span class="built_in">console</span>.log(arr.length)</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">return</span> fn2</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">var</span> f = fn1()</span><br><span class="line">		f()</span><br><span class="line">		</span><br><span class="line">		f = <span class="literal">null</span> <span class="comment">//让内部函数成为垃圾对象 ----&gt; 回收闭包</span></span><br><span class="line">	&lt;<span class="regexp">/script&gt;</span></span><br></pre></td></tr></table></figure>

<ol start="8">
<li>面试题</li>
</ol>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">&lt;script type=<span class="string">"text/javascript"</span>&gt;</span><br><span class="line">		<span class="comment">//1</span></span><br><span class="line">		<span class="keyword">var</span> name = <span class="string">'The Window'</span></span><br><span class="line">		<span class="keyword">var</span> object = &#123;</span><br><span class="line">			name: <span class="string">'My Object'</span>,</span><br><span class="line">			getNameFunc: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">				<span class="keyword">return</span> <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">					<span class="keyword">return</span> <span class="keyword">this</span>.name</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		alert(object.getNameFunc()()) <span class="comment">// The Window</span></span><br><span class="line">		</span><br><span class="line">		<span class="comment">//2</span></span><br><span class="line">		<span class="keyword">var</span> name2 = <span class="string">'The Window'</span></span><br><span class="line">		<span class="keyword">var</span> object2 = &#123;</span><br><span class="line">			name2: <span class="string">'My Object'</span>,</span><br><span class="line">			getNameFunc: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">				<span class="keyword">var</span> that = <span class="keyword">this</span></span><br><span class="line">				<span class="keyword">return</span> <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">					<span class="keyword">return</span> that.name2</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		alert(object2.getNameFunc()()) <span class="comment">//My Object</span></span><br><span class="line">&lt;<span class="regexp">/script&gt;</span></span><br></pre></td></tr></table></figure>
<p>这个应该不难理解，就是this的绑定问题，第一个返回的函数通过window对象调用的，所以this指向window，第二个保存了第一次调用this的值，指向的是object2这个对象</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">&lt;script type=<span class="string">"text/javascript"</span>&gt;</span><br><span class="line">		<span class="function"><span class="keyword">function</span> <span class="title">fun</span>(<span class="params">n,o</span>)</span>&#123;</span><br><span class="line">			<span class="built_in">console</span>.log(o)</span><br><span class="line">			<span class="keyword">return</span> &#123;</span><br><span class="line">				fun: <span class="function"><span class="keyword">function</span>(<span class="params">m</span>)</span>&#123;</span><br><span class="line">					<span class="keyword">return</span> fun(m,n)</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">var</span> a = fun(<span class="number">0</span>)</span><br><span class="line">		a.fun(<span class="number">1</span>)</span><br><span class="line">		a.fun(<span class="number">2</span>)</span><br><span class="line">		a.fun(<span class="number">3</span>)  <span class="comment">//undefined 0 0 0</span></span><br><span class="line">		<span class="keyword">var</span> b = fun(<span class="number">0</span>).fun(<span class="number">1</span>).fun(<span class="number">2</span>).fun(<span class="number">3</span>) <span class="comment">//undefined 0 1 2</span></span><br><span class="line">		<span class="keyword">var</span> c = fun(<span class="number">0</span>).fun(<span class="number">1</span>)</span><br><span class="line">		c.fun(<span class="number">2</span>)</span><br><span class="line">		c.fun(<span class="number">3</span>) <span class="comment">//undefined 0 1 1</span></span><br><span class="line">&lt;<span class="regexp">/script&gt;</span></span><br></pre></td></tr></table></figure>
<p>这道题挺过分的，它考察的就是那个闭包变量的值，就是o,而o是n赋值的，n是m赋值的,关键在于它的调用有没有产生新的闭包，如果没产生新的闭包，那么原来产生闭包的变量的值不会改变<br>好了，我的分享就到这里了，谢谢大家</p>

      
      <!-- reward -->
      
      <div id="reward-btn">
        打赏
      </div>
      
    </div>
    
    
      <!-- copyright -->
      
        <div class="declare">
          <ul class="post-copyright">
            <li>
              <i class="ri-copyright-line"></i>
              <strong>版权声明： </strong s>
              本博客所有文章除特别声明外，均采用 <a href="https://www.apache.org/licenses/LICENSE-2.0.html" rel="external nofollow"
                target="_blank">Apache License 2.0</a> 许可协议。转载请注明出处！
            </li>
          </ul>
        </div>
        
    <footer class="article-footer">
      
          
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=https://lockme.gitee.io/2020/06/13/Advanced-JS-two-md/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>
      
      

    </footer>

  </div>

  
  
  <nav class="article-nav">
    
      <a href="/2020/06/13/Advanced-JS-three-md/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            Advanced_JS_three.md
          
        </div>
      </a>
    
    
      <a href="/2020/06/13/Advanced-JS-one-md/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">Advanced_JS_one.md</div>
      </a>
    
  </nav>


  

  
  
<!-- valine评论 -->
<div id="vcomments-box">
    <div id="vcomments">
    </div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src='https://cdn.jsdelivr.net/npm/valine@1.3.10/dist/Valine.min.js'></script>
<script>
    new Valine({
        el: '#vcomments',
        app_id: 'pmaQYnbUE9O0H1hiYKLiec4R-gzGzoHsz',
        app_key: 'LMujtI79W4vgEQ3Az5IMo50y',
        path: window.location.pathname,
        notify: 'false',
        verify: 'false',
        avatar: 'mp',
        placeholder: '给我的文章加点评论吧~',
        recordIP: true
    });
    const infoEle = document.querySelector('#vcomments .info');
    if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
        infoEle.childNodes.forEach(function (item) {
            item.parentNode.removeChild(item);
        });
    }
</script>
<style>
    #vcomments-box {
        padding: 5px 30px;
    }

    @media screen and (max-width: 800px) {
        #vcomments-box {
            padding: 5px 0px;
        }
    }

    #vcomments-box #vcomments {
        background-color: #fff;
    }

    .v .vlist .vcard .vh {
        padding-right: 20px;
    }

    .v .vlist .vcard {
        padding-left: 10px;
    }
</style>

  

  
  
  

</article>
</section>
      <footer class="footer">
  <div class="outer">
    <ul class="list-inline">
      <li>
        &copy;
        2020
        月下ctrlc
      </li>
      <li>
        
        Powered by
        
        
        <a href="https://hexo.io" target="_blank">Hexo</a> Theme <a href="https://github.com/Shen-Yu/hexo-theme-ayer" target="_blank">Ayer</a>
        
      </li>
    </ul>
    <ul class="list-inline">
      <li>
        
        
        <span>
  <i>PV:<span id="busuanzi_value_page_pv"></span></i>
  <i>UV:<span id="busuanzi_value_site_uv"></span></i>
</span>
        
      </li>
      
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1278069914&amp;web_id=1278069914'></script>
        
      </li>
    </ul>
  </div>
</footer>
      <div class="to_top">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>
      </div>
    </main>
    <aside class="sidebar">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/avatar.jpg" alt="Small Bridge"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags/Vue/">Vue</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="http://shenyu-vip.lofter.com" target="_blank" rel="noopener">主题作者相册</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/2020/03/18/about">关于我</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="Search">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>拿钱砸死我吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="/images/alipay.png">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="/images/wechat.png">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
    
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/share.js"></script>


<script src="/js/lazyload.min.js"></script>


<script>
  try {
    var typed = new Typed("#subtitle", {
      strings: ['面朝大海，春暖花开', '愿你一生努力，一生被爱', '想要的都拥有，得不到的都释怀'],
      startDelay: 0,
      typeSpeed: 200,
      loop: true,
      backSpeed: 100,
      showCursor: true
    });
  } catch (err) {
  }

</script>




<script src="/js/tocbot.min.js"></script>

<script>
  // Tocbot_v4.7.0  http://tscanlin.github.io/tocbot/
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: 'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto',
    onClick: (e) => {
      $('.toc-link').removeClass('is-active-link');
      $(`a[href=${e.target.hash}]`).addClass('is-active-link');
      $(e.target.hash).scrollIntoView();
      return false;
    }
  });
</script>



<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/js/ayer.js"></script>



<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>


<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
      tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
  });

  MathJax.Hub.Queue(function() {
      var all = MathJax.Hub.getAllJax(), i;
      for(i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
      }
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/mathjax@2.7.6/unpacked/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script>
  var ayerConfig = {
    mathjax: true
  }
</script>



<script src="/js/busuanzi-2.3.pure.min.js"></script>



<script type="text/javascript" src="https://js.users.51.la/20544303.js"></script>


    
  </div>
</body>

</html>